package com.dime.basic.service.impl;

import com.dime.basic.api.domain.*;
import com.dime.basic.api.model.*;
import com.dime.basic.mapper.*;
import com.dime.basic.service.IOccupationalBasicService;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.constant.DictConstant;
import com.ruoyi.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class OccupationalBasicService implements IOccupationalBasicService {

    @Autowired
    private DimeBasemaincheckconclusionMapper conclusionphraseMapper;

    @Autowired
    private DimeBasecontraindicationMapper contraindictionMapper;

    @Autowired
    private DimeBasediseaseMapper diseaseMapper;

    @Autowired
    private DimeBasegroupMapper groupMapper;

    @Autowired
    private DimeBaseharmMapper harmMapper;

    @Autowired
    private DimeBasehealthadviceMapper healthAdviceMapper;

    @Autowired
    private DimeBasemaincheckconclusionadviceMapper standardProcessAdviceMapper;

    @Autowired
    private DimeBaseoccupationaldiseaeMapper occupationDiseaseMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private DimeBaseOccupationalMapper occupationalMapper;

    @Autowired
    private RemoteDictService dictService;

    @Autowired
    private DimeBasediagnosisstandardMapper dimeBasediagnosisstandardMapper;

    @Autowired
    private DimeBaseevaluationMapper dimeBaseevaluationMapper;

    public OccupationalBasic getOccupationalBasic(){

        OccupationalBasic re = new OccupationalBasic();
        re.setConclusionPhrases(conclusionphraseMapper.selectDimeBasemaincheckconclusionList(new DimeBasemaincheckconclusion()));
        re.setContraindictions(contraindictionMapper.selectDimeBasecontraindicationList(new DimeBasecontraindication()));
        re.setDiseases(diseaseMapper.selectDimeBasediseaseList(new DimeBasedisease()));
        re.setGroups(groupMapper.selectDimeBasegroupList(new DimeBasegroup()));
        re.setHarms(harmMapper.selectDimeBaseharmList(new DimeBaseharm()));
        re.setHealthAdvices(healthAdviceMapper.selectDimeBasehealthadviceList(new DimeBasehealthadvice()));
        re.setStandardProcessAdvices(standardProcessAdviceMapper.selectDimeBasemaincheckconclusionadviceList(new DimeBasemaincheckconclusionadvice()));
        re.setSuspectedOccupationDiseases(occupationDiseaseMapper.selectDimeBaseoccupationaldiseaeList(new DimeBaseoccupationaldiseae()));
        re.setDiagnosisStandardBasics(dimeBasediagnosisstandardMapper.selectDimeBasediagnosisstandardList(new DimeBasediagnosisstandard()));
        re.setEvaluationBasics(dimeBaseevaluationMapper.selectDimeBaseevaluationList(new DimeBaseevaluation()));
        return re;
    }

    public HarmDiseaseCriterion getHarmDiseaseCriterions(HarmWorkStatus harmWorkStatus){

        List<SysDictData> dictDatas = dictService.getDictType(DictConstant.DICT_WORKSTATUS).getData();

          List<HarmDiseaseCriterion> diseases = occupationalMapper.selectHarmDiseaseCriterionList(harmWorkStatus);
        HarmDiseaseCriterion disease = null;
          if(diseases != null && diseases.size() > 0) {
               disease = diseases.get(0);
              String workStatusName = dictDatas.stream()
                      .filter(t -> Objects.equals(t.getDictValue(), harmWorkStatus.getWorkStatus()))
                      .findFirst().get().getDictLabel();
              disease.setWorkStatusName(workStatusName);
          }
        return disease;
    }

    public HarmContraCriterion getHarmContraCriterions(HarmWorkStatus harmWorkStatus) {
        List<SysDictData> dictDatas = dictService.getDictType(DictConstant.DICT_WORKSTATUS).getData();
        List<HarmContraCriterion> contras = occupationalMapper.selectHarmContraCriterionList(harmWorkStatus);
        HarmContraCriterion contraCriterion = null;
        if (contras != null && contras.size() > 0) {
            contraCriterion = contras.get(0);
            String workStatusName = dictDatas.stream()
                    .filter(t -> Objects.equals(t.getDictValue(), harmWorkStatus.getWorkStatus()))
                    .findFirst().get().getDictLabel();
            contraCriterion.setWorkStatusName(workStatusName);
        }
        return contraCriterion;
    }

    public List<HarmDiseaseCriterion> getHarmDiseaseCriterionsByList(List<HarmWorkStatus> harmWorkStatus){



        List<HarmDiseaseCriterion> diseases = occupationalMapper.selectDiseaseCriterionListByList(harmWorkStatus);

        return diseases;
    }

    public ArrayList<HarmContraCriterion> getHarmContraCriterionsByList(List<HarmWorkStatus> harmWorkStatus) {

        ArrayList<HarmContraCriterion> contras = new ArrayList<>(occupationalMapper.selectContraCriterionListByList(harmWorkStatus));

        return contras;
    }
}
